package domainLogic;

import helperClasses.Book;
import helperClasses.ClassInfo;
import helperClasses.Member;

import java.util.Vector;

import database.Database;

public class DomainLogicReal implements DomainLogic
{
	Database db;
	Member currentMember;
	
	public DomainLogicReal(Database db) throws Exception
	{
		if(db == null)
		{
			throw new Exception("Must have a database");
		}
		this.db = db;
	}

	@Override
	public boolean isMember(String name, String password) {
		if(password == null)
		{
			return db.isMember(name, password);
		}
		if(db.isMember(name, password))
		{
			currentMember = db.getMember(new Member(name, password));
			return true;
		}
		return false;

	}

	@Override
	public Vector<Vector<Object>> searchBooks(String search)
	{
		return db.searchBook(search);
	}

	@Override
	public void addCourse(ClassInfo cla) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String buyBook(Book book) {
		return db.buyBook(book);
		
	}

	@Override
	public void cancelPost(Book book) {
		db.deleteBook(book);
		
	}

	@Override
	public void deleteAccount(Member member) {
		db.removeMember(member);
		
	}

	@Override
	public void deleteCourse(ClassInfo cla) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void editProfile(Member member) {
		db.updateMember(member);
	}

	@Override
	public Member getCurrentMember() {
		// TODO Auto-generated method stub
		return currentMember;
	}

	@Override
	public void logout() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postBook(Book book) {
		db.addBook(book);
		
	}

	@Override
	public void registerAccount(Member member){
		if(db.addMember(member))
			currentMember = member;
	}

	@Override
	public void setCurrentMember(Member member) {
		currentMember = db.getMember(member);
		
	}

	@Override
	public void resetCurrentMember() {
		currentMember = null;
	}

	@Override
	public void close() {
		db.onClose();
	}
	
	public boolean isAdmin() {
		return currentMember.getType() == 1 ? true : false;
	}

	@Override
	public Vector<Vector<Object>> getUserList(String name) {
		return db.getUserList(name);
	}

	@Override
	public Vector<Vector<Object>> getBookList(String name) {
		// TODO Auto-generated method stub
		return db.getBookList(this.currentMember, name);
	}

}

